def jwt_response_payload_handler(token, user=None, request=None):
    return {
        'token': token,
        'user_id':user.id,
        'username':user.username
    }


# 实现除了用户名登录外，还能用手机号登录
from django.contrib.auth.backends import ModelBackend
import re
from apps.users.models import User

# 定义一个方法，使用正则来匹配手机号，如果不匹配，那么就是用户名
def get_user_by_account(username):
    try:
        if re.match(r'1[3-9]\d{9}', username):
            # 是手机号
            user = User.objects.get(mobile=username)
        else:
            # 是用户名
            user = User.objects.get(username=username)
    except User.DoesNotExist:
        user = None

    return user

# 继承自这个类是因为可以少些一个get_user方法.....
class LoginMobileUsernameModelBackend(ModelBackend):

    # 重写方法
    def authenticate(self, request, username=None, password=None, **kwargs):
        """
        思路:
        用户输入用户名(手机号) 之后,
        我们根据 用户输入的用户名(手机号) 和密码 进行校验
        如果成功 返回user
        """
        user = get_user_by_account(username)
        if user is not None and user.check_password(password):
            return user
        return None

# 与上一个类的功能一样，只是继承的类不同
class SettingsBackend(object):
    """
    Authenticate against the settings ADMIN_LOGIN and ADMIN_PASSWORD.

    Use the login name and a hash of the password. For example:

    ADMIN_LOGIN = 'admin'
    ADMIN_PASSWORD = 'pbkdf2_sha256$30000$Vo0VlMnkR4Bk$qEvtdyZRWTcOsCnI/oQ7fVOu1XAURIZYoOZ3iq8Dr4M='
    """

    def authenticate(self, request, username=None, password=None):
        user = get_user_by_account(username=username)

        # user.check_password(password 通过 AbstractUser 中的方法检测密码
        if user is not None and user.check_password(password):
            return user

        return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None